// ==UserScript==
// @name           Yahoo Fantasy Quick Select
// @namespace      http://glenncarr.com/greasemonkey/yahoofantasy
// @include        *fantasysports.yahoo.com/*
// @version        1.0.0 (23-May-2007)
// @author         (c) 2007 Glenn Carr <glenn at glenncarr dot com>
// ==/UserScript==
/*
 * Most of the credit for this goes to Ryan Grove <ryan@wonko.com> and his excellent Jyte ClaimInfo script:
 *    http://userscripts.org/scripts/show/9216
 *
 * Updates:
 */

(function() {

var YAHOO, yud, yue, yut, yum;

String.prototype.trim = function () {
  return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}

String.prototype.stripTags = function() {
  return this.replace(/<\/?[^>]+>/gi,'');
}

var QuickSelect = function () {

  // -- Private Variables ------------------------------------------------------
  var WORKING_IMG_URL = "data:image/gif,GIF89a%0A%00%0A%00%91%03%00%CC%CC%CC%FFff%FF%00%00%FF%FF%FF!%FF%" +
                        "0BNETSCAPE2.0%03%01%00%00%00!%F9%04%05%00%00%03%00%2C%00%00%00%00%0A%00%0A%00%00%02%17%9C'r%06%80%1A%" +
                        "02s'%AE%3Bqk%9A%E2%C3%81%14Gz%D9Q%00%00!%F9%04%05%00%00%03%00%2C%01%00%00%00%08%00%03%00%00%02%0A%9C%" +
                        "136%22%83%03%00S%10%14%00!%F9%04%05%00%00%03%00%2C%00%00%00%00%06%00%06%00%00%02%0C%9C%070%11%A8%7C%A" +
                        "2%11%22%D2X%00%00!%F9%04%05%00%00%03%00%2C%00%00%01%00%03%00%08%00%00%02%0A%1C%608%13%C1%BE%96%10c%16" +
                        "%00!%F9%04%05%00%00%03%00%2C%00%00%04%00%06%00%06%00%00%02%0A%04%86c%C9%1C%E1%A0%10l%16%00!%F9%04%05%" +
                        "00%00%03%00%2C%01%00%07%00%08%00%03%00%00%02%0A%04%86%23b%13%A1Dz%A9%00%00!%F9%04%05%00%00%03%00%2C%0" +
                        "4%00%04%00%06%00%06%00%00%02%0C%9C'r%A8%BB%11%06%00%03Jz%0A%00!%F9%04%09%00%00%03%00%2C%07%00%01%00%0" +
                        "3%00%08%00%00%02%0A%94f%A3%1A1%BD%00%18r%14%00%3B";
  var leagueMenu = null;

  return {
    // -- Public Methods -------------------------------------------------------

    init: function () {
      var root = document.getElementById( 'yspteammh' );
      var b = document.createElement( 'SPAN' );
      b.id = 'qs_leaguemenuroot';
      b.innerHTML = '&nbsp;<b>&raquo;</b>';
      root.appendChild( b );

      leagueMenu = new YAHOO.widget.Menu("qs_leaguemenu", { visible:false, x:yud.getX(b), y:yud.getY(b) + b.offsetHeight } );
      YAHOO.util.Event.addListener(document.getElementById('qs_leaguemenuroot'), "mouseover", this.showLeagueMenu, this, true);

      GM_xmlhttpRequest({
        method: 'GET',
        url   : 'http://fantasysports.yahoo.com/',
        onload: this.handleResponse
      });
    },

    handleResponse: function(response) {
      var iframe = document.createElement('IFRAME');
      iframe.innerHTML = response.responseText;
      var divs = iframe.getElementsByTagName( 'div' );
      var teams = new Array();
      for ( var iDiv = 0; iDiv < divs.length; iDiv++ )
      {
        if ( divs[ iDiv ].getAttribute('class') == 'bd' )
        {
          var lis = divs[ iDiv ].getElementsByTagName( 'li' );
          for ( var iTeam = 0; iTeam < lis.length; iTeam++ )
          {
            var a = lis[ iTeam ].getElementsByTagName( 'a' )[ 0 ];
            teams.push( { href:a, text:a.innerHTML } );
          }
          break;
        }
      }

      for ( var iTeam = 0; iTeam < teams.length; iTeam++ )
      {
        var href = teams[ iTeam ].href;
        var isTeam = /(\/\d+){2}$/.test( href );
        leagueMenu.addItem( new YAHOO.widget.MenuItem( ( isTeam ? '' : ' - ' ) + teams[ iTeam ].text, { url:href, strongemphasis:isTeam, emphasis:!isTeam } ) );
      }
      leagueMenu.render(document.body);
    },

    showLeagueMenu: function(e) {
      var t = yue.getTarget(e);
      leagueMenu.show();
    },

    /**
     * Loads the YUI Dom, Event, and Menu libraries and calls
     * .init() when they're finished loading.
     */
    loadYUI: function () {

      var yuiCssFonts = document.createElement('link'),
          yuiCssMenu = document.createElement('link'),
          yuiDomEvent = document.createElement('script'),
          yuiContainer = document.createElement('script'),
          yuiMenu = document.createElement('script');

      yuiCssFonts.rel = 'stylesheet';
      yuiCssFonts.type = 'text/css';
      yuiCssFonts.href = 'http://yui.yahooapis.com/2.2.2/build/fonts/fonts-min.css';

      yuiCssMenu.rel = 'stylesheet';
      yuiCssMenu.type = 'text/css';
      yuiCssMenu.href = 'http://yui.yahooapis.com/2.2.2/build/menu/assets/menu.css';

      yuiDomEvent.src = 'http://yui.yahooapis.com/2.2.2/build/yahoo-dom-event/yahoo-dom-event.js';
      yuiContainer.src = 'http://yui.yahooapis.com/2.2.2/build/container/container_core-min.js';
      yuiMenu.src = 'http://yui.yahooapis.com/2.2.2/build/menu/menu-min.js';

      document.body.appendChild(yuiCssFonts);
      document.body.appendChild(yuiCssMenu);
      document.body.appendChild(yuiDomEvent);
      document.body.appendChild(yuiContainer);
      document.body.appendChild(yuiMenu);

      var loadInterval = setInterval(function () {
        if ( !unsafeWindow.YAHOO || !unsafeWindow.YAHOO.widget.Menu ) {
          return;
        }

        clearInterval(loadInterval);

        YAHOO = unsafeWindow.YAHOO;
        yut   = YAHOO.util;
        yum   = YAHOO.widget.Menu;
        yud   = yut.Dom;
        yue   = yut.Event;

        QuickSelect.init();
      }, 50);
    },
  };
}();

QuickSelect.loadYUI();
})();